package ${basePackage}.web.${className?lower_case};

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.Servlets;

import com.cnksi.dwz.web.dto.Pagination;
import com.cnksi.poi.PoiExportExcel;
import com.cnksi.poi.annotation.DescriptionInfo;
import ${basePackage}.entity.${className};
import ${basePackage}.service.${className?lower_case}.${className}Service;
import ${basePackage}.web.WebHelper;

/**
 * ${className} 管理的Controller, 使用Restful风格的Urls:
 * 
 * List page     : GET  /${className?lower_case}/
 * Create page   : GET  /${className?lower_case}/create 
 * Create action : POST /${className?lower_case}/create 
 * Update page   : GET  /${className?lower_case}/update/{${pkFieldInfo.field}}
 * Update action : POST /${className?lower_case}/update
 * Delete action : GET  /${className?lower_case}/delete/{${pkFieldInfo.field}}
 * Export action : GET  /${className?lower_case}/export
 * 
 * @author creator
 *
 */
@Controller
@RequestMapping(value = "/${className?lower_case}")
public class ${className}Controller
{

	public static final String LIST_REL = "${className?lower_case}-list";

	public static final String FORM_REL = "${className?lower_case}-form";

	@Autowired
	private ${className}Service ${className?lower_case}Service;   
	
	<#if config.shiroControllerAnnotation>@RequiresPermissions("${className?lower_case}:read")</#if>
	@RequestMapping(value = "list")
	public String list(Pagination pagination, Model model, ServletRequest request)
	{

		pagination.setOrderField("${pkFieldInfo.field}");

		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");

		model.addAttribute("${className?lower_case}s", ${className?lower_case}Service.get${className}sByPage(searchParams, pagination));

		model.addAttribute("pagination", pagination);

		return "${className?lower_case}/${className?lower_case}List";
	}
	
	<#if config.shiroControllerAnnotation>@RequiresPermissions("${className?lower_case}:export-xls")</#if>
	@RequestMapping(value = "/export/xls")
	public void export(Pagination pagination, Model model, ServletRequest request, final HttpServletResponse response)
	{

		pagination.setOrderField("${pkFieldInfo.field}");

		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");

		//获得数据
		List<${className}> ${className?lower_case}List = ${className?lower_case}Service.get${className}ByPage(searchParams, pagination).getContent();

		List<DescriptionInfo> descriptionInfoList = PoiExportExcel.getInstance().parseField(${className}.class);

		PoiExportExcel.getInstance().downloadFile(response, "${className?lower_case}-list.xls", descriptionInfoList, ${className?lower_case}List);
	}

	<#if config.shiroControllerAnnotation>@RequiresPermissions("${className?lower_case}:create")</#if>
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model)
	{

		model.addAttribute("${className?lower_case}", new ${className}());

		model.addAttribute("action", "create");

		return "${className?lower_case}/${className?lower_case}Form";
	}
	
	<#if config.shiroControllerAnnotation>@RequiresPermissions("${className?lower_case}:create")</#if>
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public Object create(@Valid ${className} new${className})
	{

		${className?lower_case}Service.save${className}(new${className});

		return WebHelper.returnSuccessMapInfo(LIST_REL);
	}
	
	<#if config.shiroControllerAnnotation>@RequiresPermissions("${className?lower_case}:edit")</#if>
	@RequestMapping(value = "update/{${pkFieldInfo.field}}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("${pkFieldInfo.field}") ${pkFieldInfo.type} ${pkFieldInfo.field}, Model model)
	{

		model.addAttribute("${className?lower_case}", ${className?lower_case}Service.get${className}(${pkFieldInfo.field}));
		model.addAttribute("action", "update");
		return "${className?lower_case}/${className?lower_case}Form";
	}
	
	<#if config.shiroControllerAnnotation>@RequiresPermissions("${className?lower_case}:edit")</#if>
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid @ModelAttribute("preload${className}") ${className} ${className?lower_case})
	{

		${className?lower_case}Service.save${className}(${className?lower_case});

		return WebHelper.returnSuccessMapInfo(LIST_REL);
	}
	
	<#if config.shiroControllerAnnotation>@RequiresPermissions("${className?lower_case}:delete")</#if>
	@RequestMapping(value = "delete/{${pkFieldInfo.field}}")
	@ResponseBody
	public Object delete(@PathVariable("${pkFieldInfo.field}") Integer ${pkFieldInfo.field})
	{

		${className?lower_case}Service.delete${className}(${pkFieldInfo.field});

		return WebHelper.returnRefreshMapInfo(LIST_REL);
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出${className}对象,再把Form提交的内容绑定到该对象上。
	 * 
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preload${className}")
	public ${className} get${className}(@RequestParam(value = "${pkFieldInfo.field}", required = false) ${pkFieldInfo.type} ${pkFieldInfo.field})
	{

		if (${pkFieldInfo.field} != null)
		{
			return ${className?lower_case}Service.get${className}(${pkFieldInfo.field});
		}
		return null;
	}
}

  